@model AdminIndexViewModel
@using Microsoft.AspNetCore.Http
@using OrchardCore.Environment.Shell
@using OrchardCore.Environment.Shell.Models

@{
    var originalPathBase = Context.Features.Get<ShellContextFeature>().OriginalPathBase;

    int startIndex = (Model.Pager.Page - 1) * (Model.Pager.PageSize) + 1;
    int endIndex = startIndex + Model.ShellSettingsEntries.Count - 1;
}

@functions
{
    public string GetEncodedUrl(ShellSettingsEntry shellSettingsEntry, PathString originalPathBase)
    {
        var shellSettings = shellSettingsEntry.ShellSettings;

        var requestHost = Context.Request.Host;
        var host = shellSettings.RequestUrlHost?.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() ?? requestHost.Host;

        var port = requestHost.Port;

        if (port.HasValue)
        {
            host += ":" + port;
        }

        var hostString = new HostString(host);

        var pathString = originalPathBase;

        if (!string.IsNullOrEmpty(shellSettings.RequestUrlPrefix))
        {
            pathString = pathString.Add('/' + shellSettings.RequestUrlPrefix);
        }

        QueryString queryString;

        if (!string.IsNullOrEmpty(shellSettingsEntry.Token))
        {
            queryString = QueryString.Create("token", shellSettingsEntry.Token);
        }

        return $"{Context.Request.Scheme}://{hostString + pathString + queryString}";
    }

    public string GetDisplayUrl(ShellSettingsEntry shellSettingsEntry, PathString originalPathBase)
    {
        var shellSettings = shellSettingsEntry.ShellSettings;

        var requestHost = Context.Request.Host;
        var host = shellSettings.RequestUrlHost?.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault() ?? requestHost.Host;

        var port = requestHost.Port;

        if (port.HasValue)
        {
            host += ":" + port;
        }

        var pathString = originalPathBase;

        if (!string.IsNullOrEmpty(shellSettings.RequestUrlPrefix))
        {
            pathString = pathString.Add('/' + shellSettings.RequestUrlPrefix);
        }

        return $"{Context.Request.Scheme}://{host + pathString.Value}";
    }
}

<script asp-name="bootstrap-select" depends-on="admin" at="Foot"></script>
<style asp-name="bootstrap-select"></style>

<h1>@RenderTitleSegments(T["Tenants"])</h1>
<form asp-action="Index" method="post">
    <input type="submit" name="submit.Filter" id="submitFilter" class="d-none" />
    <input type="submit" name="submit.BulkAction" class="d-none" />
    <input name="BulkAction" type="hidden" />

    <div class="row">
        <div class="form-group col-xl-4 col-sm-6">
            <div class="input-group input-group-sm filter-options">
                <div class="input-group-prepend">
                    <button class="btn btn-light btn-sm dropdown-toggle" style="border:1px solid lightgrey" type="button" data-toggle="dropdown" id="filter-dropdown" aria-haspopup="true" aria-expanded="false">
                        @T["Filters"]
                    </button>
                    <div class="dropdown-menu" aria-labelledby="filter-dropdown">
                        <a class="dropdown-item" href="?Options.Filter=Running">@T["Only running tenants"]</a>
                        <a class="dropdown-item" href="?Options.Filter=Disabled">@T["Only disabled tenants"]</a>
                        <a class="dropdown-item" href="?Options.Filter=Uninitialized">@T["Only uninitialized tenants"]</a>
                    </div>
                </div>
                <input id="search-box" asp-for="Options.Search" class="form-control" placeholder="@T["Search"]" type="search" autofocus />
            </div>
        </div>
        <div class="form-group col-xl-8 col-sm-6">
            <div class="btn-group float-right">
                <a asp-route-action="Create" class="btn btn-sm btn-primary float-rights" role="button">@T["Add Tenant"]</a>
            </div>
        </div>
    </div>
    <ul class="list-group with-checkbox">
        <li class="list-group-item bg-light">
            <div class="row">
                <div class="form-group col mb-n1 text-nowrap">
                    <div class="custom-control custom-checkbox mt-2 mr-n2">
                        <input type="checkbox" class="custom-control-input" id="select-all">
                        <label class="custom-control-label" for="select-all" id="select-all-label" title="@T["Select All"]"></label>
                        <label id="items" for="select-all">@T.Plural(Model.ShellSettingsEntries.Count, "1 item", "{0} items")<span class="text-muted" title="@T["Items {0} to {1}", startIndex, endIndex]">@T.Plural((int)Model.Pager.TotalItemCount, " / {0} item in total", " / {0} items in total")</span></label>
                        <label id="selected-items" class="text-muted" for="select-all"></label>
                    </div>
                </div>
                <div class="form-group col-2 mb-n1" style="display:none" id="actions">
                    <span class="dropdown float-right mt-1">
                        <button class="btn btn-sm btn-light dropdown-toggle" type="button" id="bulk-action-menu-button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            @T["Actions"]
                        </button>
                        <span class="dropdown-menu dropdown-menu-right" aria-labelledby="bulk-action-menu-button">
                            @foreach (var item in Model.Options.TenantsBulkAction)
                            {
                                <a class="dropdown-item" href="javascript:void(0)" data-action="@item.Value" data-title="@T["Bulk Action"]" data-message="@T["Are you sure you want to {0} these items?", @item.Text.ToLower()]">@item.Text</a>
                            }
                        </span>
                    </span>
                </div>
                <div class="form-group col mb-n1 filter">
                    <div class="btn-group float-right mt-1">
                        <select asp-for="Options.Filter" asp-items="@Model.Options.TenantsStates" class="selectpicker show-tick mr-2" data-header="@T["Filter by state"]" data-width="fit" data-selected-text-format="static" data-dropdown-align-right="true" title="@T["State"]" data-style="btn-sm"></select>
                        <select asp-for="Options.OrderBy" asp-items="@Model.Options.TenantsSorts" class="selectpicker show-tick" data-header="@T["Sort by"]" data-width="fit" data-selected-text-format="static" data-dropdown-align-right="true" title="@T["Sort"]" data-style="btn-sm"></select>
                    </div>
                </div>
            </div>
        </li>
        @if (Model.ShellSettingsEntries.Count > 0)
        {
            @foreach (var entry in Model.ShellSettingsEntries)
            {
                <li class="list-group-item">
                    <div class="custom-control custom-checkbox float-left">
                        <input type="checkbox" class="custom-control-input" value="@entry.Name" name="tenantNames" id="tenantNames-@entry.Name">
                        <label class="custom-control-label" for="tenantNames-@entry.Name"></label>
                    </div>
                    <div class="properties">
                        <div class="related">
                            <a asp-action="Edit" asp-route-id="@entry.Name" class="btn btn-primary btn-sm">@T["Edit"]</a>
                            @if (!entry.IsDefaultTenant && entry.ShellSettings.State == TenantState.Running)
                            {
                                <a asp-action="Disable" asp-route-id="@entry.Name" class="btn btn-danger btn-sm" itemprop="UnsafeUrl">@T["Disable"]</a>
                            }
                            @if (entry.ShellSettings.State == TenantState.Disabled)
                            {
                                <a asp-action="Enable" asp-route-id="@entry.Name" class="btn btn-success btn-sm" itemprop="UnsafeUrl">@T["Enable"]</a>
                            }
                            @if (entry.ShellSettings.State == TenantState.Uninitialized)
                            {
                                <a class="btn btn-info btn-sm" id="btn-setup-@entry.Name" href="@GetEncodedUrl(entry, originalPathBase)">@T["Setup"]</a>
                            }
                            <a asp-action="Reload" asp-route-id="@entry.Name" class="btn btn-secondary btn-sm" itemprop="UnsafeUrl">@T["Reload"]</a>
                        </div>
                        <a href="@GetEncodedUrl(entry, originalPathBase)">@entry.Name</a>
                        <code class="hint">@GetDisplayUrl(entry, originalPathBase)</code>
                        @if (!string.IsNullOrEmpty(entry.Description))
                        {
                            <p class="mb-0"><small class="text-muted">@entry.Description</small></p>
                        }
                        <div class="metadata">
                            @switch (entry.ShellSettings.State)
                            {
                                case TenantState.Disabled:
                                    <span class="badge badge-danger">@T["Disabled"]</span>
                                    break;
                                case TenantState.Initializing:
                                    <span class="badge badge-info">@T["Initializing"]</span>
                                    break;
                                case TenantState.Invalid:
                                    <span class="badge badge-warning">@T["Invalid"]</span>
                                    break;
                                case TenantState.Running:
                                    <span class="badge badge-success">@T["Running"]</span>
                                    break;
                                case TenantState.Uninitialized:
                                    <span class="badge badge-secondary">@T["Uninitialized"]</span>
                                    break;
                            }
                        </div>
                    </div>
                </li>
            }
        }
        else
        {
            <li class="list-group-item">
                <div class="alert alert-info">
                    @T["<strong>Nothing here!</strong> Your search returned no results."]
                </div>
            </li>
        }
    </ul>
</form>

@await DisplayAsync(Model.Pager)

<script at="Foot" type="text/javascript">
    $(function () {
        $('.selectpicker').selectpicker();

        var actions = $("#actions");
        var items = $("#items");
        var filters = $(".filter");
        var selectAllCtrl = $("#select-all");
        var selectedItems = $("#selected-items");
        var itemsCheckboxes = $(":checkbox[name='tenantNames']");

        $(".filter-options select, .filter-options input").on("change", function () {
            $("[name='submit.Filter']").click();
        });

        $('.selectpicker').on('changed.bs.select', function (e, clickedIndex, isSelected, previousValue) {
            $("[name='submit.Filter']").click();
        });

        $(".dropdown-menu .dropdown-item").filter(function() {
            return $(this).data("action");
        }).on("click", function () {
            if ($(":checkbox[name='tenantNames']:checked").length > 1) {
                var $this = $(this);
                confirmDialog({title: $this.data('title'), message: $this.data('message'), callback: function(r) {
                    if (r) {
                        $("[name='BulkAction']").val($this.data("action"));
                        $("[name='submit.BulkAction']").click();
                    }
                }});
            }
        });

        function displayActionsOrFilters() {
            if ($(":checkbox[name='tenantNames']:checked").length > 1) {
                actions.show();
                filters.hide();
                selectedItems.show();
                items.hide();
            }
            else {
                actions.hide();
                filters.show();
                selectedItems.hide();
                items.show();
            }
        }

        selectAllCtrl.click(function(){
            itemsCheckboxes.not(this).prop("checked", this.checked);
            selectedItems.text($(":checkbox[name='tenantNames']:checked").length + ' @T["selected"]');
            displayActionsOrFilters();
        });

        itemsCheckboxes.on("click", function () {
            var itemsCount = $(":checkbox[name='tenantNames']").length;
            var selectedItemsCount = $(":checkbox[name='tenantNames']:checked").length;

            selectAllCtrl.prop("checked", selectedItemsCount == itemsCount);
            selectAllCtrl.prop("indeterminate", selectedItemsCount > 0 && selectedItemsCount < itemsCount);

            selectedItems.text(selectedItemsCount + ' @T["selected"]');
            displayActionsOrFilters();
        });
    });
</script>
